//
//  CustomMainCell.m
//  19--Homework 汽车之家
//
//  Created by 冀永金 on 16/8/9.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "CustomMainCell.h"
#import "MainCarCollectionViewCell.h"
#import "CarMainModel.h"
@interface CustomMainCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic,strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *marr;
@end
@implementation CustomMainCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self config];
        [self handleData];
    }
    return self;
}
- (void)handleData {
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSMutableURLRequest *requestHot = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://adnewnc.app.autohome.com.cn/autov5.7.0/ad/infoad.ashx?appid=2&platform=1&version=6.2.0&networkid=0&adtype=1&provinceid=110000&lng=121.550888&lat=38.889660&pageindex=1&deviceid=d1ecb940123d620cb05a1b2b0a00da8bdd5e4fbe&idfa=981CD0F4-F1F0-4D0F-9C5E-58280F40E4B0&devicebrand=apple&devicemodel=iPhone&gps_city=210200&pageid=d1ecb940123d620cb05a1b2b0a00da8bdd5e4fbe1470913307169&isretry=0"]];
    requestHot.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:requestHot completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSDictionary *dicOfHot = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers  error:nil];
            self.marr = [NSMutableArray array];
            NSArray *listArray = dicOfHot[@"result"][@"list"];
            for (NSDictionary *dic in listArray) {
                CarMainModel *carModel = [CarMainModel carMianWithDic:dic];
                [self. marr addObject:carModel];
            }
            [self.collection reloadData];
        });
    }];
    [dataTask resume];
}
- (void)config {
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    //_layout.minimumInteritemSpacing = 20;
    _layout.minimumLineSpacing = 20;
    _collection = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:_layout];
    [self.contentView addSubview:_collection];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[MainCarCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    _collection.backgroundColor = [UIColor whiteColor];
   
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.collection.frame = self.bounds;
    self.layout.itemSize = CGSizeMake((self.bounds.size.width - 60) / 3, self.bounds.size.height - 10);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.marr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MainCarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
   // cell.backgroundColor = [UIColor redColor];
    CarMainModel *model = self.marr [indexPath.row];
    [cell setCarModel:model];
    return cell;
}
@end
